package wqh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.MapPosGeo;
import wqh.pojo.MapRet;
import wqh.service.MapPosGeoService;

import java.util.List;

/**
 * Created by devts on 2016/12/2.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/map_pos_geo")
public class MapPosGeoController {

    @Autowired
    private MapPosGeoService mapPosGeoService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody MapPosGeo mapPosGeo)
    {
        try {
            List<MapPosGeo> mapPosGeos = mapPosGeoService.findByGeoIdAndName(mapPosGeo.getGeo_id(),mapPosGeo.getName());
            if(mapPosGeos.size() != 0)     return MapRet.SQL_ERROR("数据已存在");
            mapPosGeoService.insert(mapPosGeo);
            return MapRet.SQL_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_ERROR();
        }
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll(@RequestParam(value = "geo_id",required = true) int id)
    {
        try {
            List<MapPosGeo> mapPosGeos = mapPosGeoService.findByGeoId(id);
            if(mapPosGeos.size() == 0) return MapRet.SQL_ERROR();
            else return MapRet.DATA(mapPosGeos);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_ERROR();
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Object delete(@RequestParam(value = "id",required = true) int id)
    {
        try {
            mapPosGeoService.deleteById(id);
            return MapRet.SQL_DELETE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_DELETE_ERROR();
        }
    }

}
